package com.learnerTest.jwt.util;

import io.jsonwebtoken.*;

import java.util.Date;

public class JwtUtils {

    public static final Long EXPIRE = 1000*60*30L;

    public static final String APP_SECRET = "learner";

    public static String  genJwt(String id,String nickname,String avatar){
        JwtBuilder builder = Jwts.builder();

        builder.setHeaderParam("alg","HS256");
        builder.setHeaderParam("type","JWT");

        builder.setId("1");
        builder.setSubject("learner-users");
        builder.setIssuedAt(new Date());
        builder.setExpiration(new Date(System.currentTimeMillis()+EXPIRE));

        builder.claim("id",id);
        builder.claim("nickname",nickname);
        builder.claim("avatar",avatar);

        builder.signWith(SignatureAlgorithm.HS256,APP_SECRET);

        return builder.compact();
    }

    public static Claims  checkJwt(String jwtToken) {
        JwtParser parser = Jwts.parser();
        Jws<Claims> claimsJws = parser.setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        return claimsJws.getBody();
    }
}
